package com.lanou.horo.zhuizhui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.activity.WebViewActivity;
import com.lanou.horo.zhuizhui.entity.HomePageItem;
import com.lanou.horo.zhuizhui.utility.clickInterface.HomePageItemClickListener;
import com.lanou.horo.zhuizhui.utility.imageLoader.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/4.
 */
public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE00 = 0;
    private static final int TYPE01 = 1;
    private static final int TYPE02 = 2;
    private static final int TYPE03 = 3;

    private LayoutInflater inflater;
    private List<HomePageItem.ListsEntity> entities;
    private ImageLoader imageLoader;
    private Context context;
    private HomePageItemClickListener itemClickListener;


    public List<HomePageItem.ListsEntity> getEntities() {
        return entities;
    }

    public void setItemClickListener(HomePageItemClickListener itemClick) {
        this.itemClickListener = itemClick;
    }

    public void setImgClickListener(HomePageItemClickListener imgClickListener) {
        this.itemClickListener = imgClickListener;
    }

    public void addData(List<HomePageItem.ListsEntity> datas) {
        entities = datas;
        notifyDataSetChanged();
    }

    public HomeAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case TYPE00:
                return new ViewHolder00(inflater.inflate(R.layout.home_page_one, null));
            case TYPE01:
                return new ViewHolder01(inflater.inflate(R.layout.home_page_two, null));
            case TYPE02:
                return new ViewHolder02(inflater.inflate(R.layout.home_page_three, null));
            case TYPE03:
                return new ViewHolder03(inflater.inflate(R.layout.home_page_one, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            HomePageItem.ListsEntity entity = entities.get(position);
            if (holder instanceof ViewHolder00) {
                List<HomePageItem.ListsEntity.ThemeListEntity> themeList = entity.getTheme_list();
                if (themeList != null && themeList.size() >= 4) {
                    ViewHolder00 h = ((ViewHolder00) holder);
                    h.homeOFimage1.setImageUrl(themeList.get(0).getCover(), imageLoader);
                    h.homeOFimage2.setImageUrl(themeList.get(1).getCover(), imageLoader);
                    h.homeOFimage3.setImageUrl(themeList.get(2).getCover(), imageLoader);
                    h.homeOFimage4.setImageUrl(themeList.get(3).getCover(), imageLoader);
                    h.homeOFpl1.setText("" + themeList.get(0).getComment_count());
                    h.homeOFpl2.setText("" + themeList.get(1).getComment_count());
                    h.homeOFpl3.setText("" + themeList.get(2).getComment_count());
                    h.homeOFpl4.setText("" + themeList.get(3).getComment_count());
                    h.homeOFzan1.setText("" + themeList.get(0).getUp_count());
                    h.homeOFzan2.setText("" + themeList.get(1).getUp_count());
                    h.homeOFzan3.setText("" + themeList.get(2).getUp_count());
                    h.homeOFzan4.setText("" + themeList.get(3).getUp_count());
                    h.homeOFtitle1.setText(themeList.get(0).getName());
                    h.homeOFtitle2.setText(themeList.get(1).getName());
                    h.homeOFtitle3.setText(themeList.get(2).getName());
                    h.homeOFtitle4.setText(themeList.get(3).getName());
                    h.v0Position = position;
                }
            }
            if (holder instanceof ViewHolder01) {
                List<HomePageItem.ListsEntity.ComicsEntity> comicsList = entity.getComics();
                if (comicsList != null && comicsList.size() >= 6) {
                    ViewHolder01 h01 = (ViewHolder01) holder;
                    h01.homeTSimage1.setImageUrl(comicsList.get(0).getCover_img(), imageLoader);
                    h01.homeTSimage2.setImageUrl(comicsList.get(1).getCover_img(), imageLoader);
                    h01.homeTSimage3.setImageUrl(comicsList.get(2).getCover_img(), imageLoader);
                    h01.homeTSimage4.setImageUrl(comicsList.get(3).getCover_img(), imageLoader);
                    h01.homeTSimage5.setImageUrl(comicsList.get(4).getCover_img(), imageLoader);
                    h01.homeTSimage6.setImageUrl(comicsList.get(5).getCover_img(), imageLoader);
                    h01.homeTStitle1.setText(comicsList.get(0).getName());
                    h01.homeTStitle2.setText(comicsList.get(1).getName());
                    h01.homeTStitle3.setText(comicsList.get(2).getName());
                    h01.homeTStitle4.setText(comicsList.get(3).getName());
                    h01.homeTStitle5.setText(comicsList.get(4).getName());
                    h01.homeTStitle6.setText(comicsList.get(5).getName());
                    h01.homeTscontent1.setText("更新至:" + comicsList.get(0).getLast_volume());
                    h01.homeTscontent2.setText("更新至:" + comicsList.get(1).getLast_volume());
                    h01.homeTscontent3.setText("更新至:" + comicsList.get(2).getLast_volume());
                    h01.homeTscontent4.setText("更新至:" + comicsList.get(3).getLast_volume());
                    h01.homeTscontent5.setText("更新至:" + comicsList.get(4).getLast_volume());
                    h01.homeTscontent6.setText("更新至:" + comicsList.get(5).getLast_volume());
                    h01.homeOFMaintitle.setText(entity.getName());
                    h01.v1Position = position;
                }
            }
            if (holder instanceof ViewHolder02) {
                List<HomePageItem.ListsEntity.ThemeListEntity> themeList = entity.getTheme_list();
                if (themeList != null && themeList.size() >= 2) {
                    ViewHolder02 h02 = (ViewHolder02) holder;
                    h02.homeTTtitle.setText(entity.getName());
                    h02.homeTTimage1.setImageUrl(themeList.get(0).getCover(), imageLoader);
                    h02.homeTTimage2.setImageUrl(themeList.get(1).getCover(), imageLoader);
                    h02.homeTTcontent1.setText(themeList.get(0).getName());
                    h02.homeTTcontent2.setText(themeList.get(1).getName());
                    h02.v2Position = position;
                }
            }
            if (holder instanceof ViewHolder03) {
                List<HomePageItem.ListsEntity.ContentListsEntity> conList = entity.getContent_lists();
                if (conList != null && conList.size() >= 4) {
                    ViewHolder03 h03 = (ViewHolder03) holder;
                    h03.homeOFpl1.setText("" + conList.get(0).getComment_count());
                    h03.homeOFpl2.setText("" + conList.get(1).getComment_count());
                    h03.homeOFpl3.setText("" + conList.get(2).getComment_count());
                    h03.homeOFpl4.setText("" + conList.get(3).getComment_count());
                    h03.homeOFzan1.setText("" + conList.get(0).getUp_count());
                    h03.homeOFzan2.setText("" + conList.get(1).getUp_count());
                    h03.homeOFzan3.setText("" + conList.get(2).getUp_count());
                    h03.homeOFzan4.setText("" + conList.get(3).getUp_count());
                    h03.homeOFtitle1.setText(conList.get(0).getTitle());
                    h03.homeOFtitle2.setText(conList.get(1).getTitle());
                    h03.homeOFtitle3.setText(conList.get(2).getTitle());
                    h03.homeOFtitle4.setText(conList.get(3).getTitle());
                    h03.homeOFimage1.setImageUrl(conList.get(0).getCovers().get(0), imageLoader);
                    h03.homeOFimage2.setImageUrl(conList.get(1).getCovers().get(0), imageLoader);
                    h03.homeOFimage3.setImageUrl(conList.get(2).getCovers().get(0), imageLoader);
                    h03.homeOFimage4.setImageUrl(conList.get(3).getCovers().get(0), imageLoader);
                    h03.homeOFMaintitle.setText(entity.getName());
                    h03.v3Position = position;
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }


    @Override
    public int getItemViewType(int position) {
        if (entities.get(position).getObj_type() == 1) {
            if (entities.get(position).getName().equals("今日追追资讯")) {
                return TYPE00;
            }
            return TYPE02;
        }
        if (entities.get(position).getObj_type() == 2) {
            return TYPE01;
        }
        return TYPE03;
    }

    public class ViewHolder00 extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView homeOFimage1, homeOFimage2, homeOFimage3, homeOFimage4;
        private TextView homeOFtitle1, homeOFtitle2, homeOFtitle3, homeOFtitle4, homeOFpl1, homeOFchange,
                homeOFpl2, homeOFpl3, homeOFpl4, homeOFzan1, homeOFzan2, homeOFzan3, homeOFzan4;
        private int v0Position;
        private CardView homeOFmore;
        private LinearLayout homefImgf, homefImgs, homefImgt, homefImgo;

        public ViewHolder00(View itemView) {
            super(itemView);
            homefImgf = (LinearLayout) itemView.findViewById(R.id.homefImgf);
            homefImgs = (LinearLayout) itemView.findViewById(R.id.homefImgs);
            homefImgt = (LinearLayout) itemView.findViewById(R.id.homefImgt);
            homefImgo = (LinearLayout) itemView.findViewById(R.id.homefImgo);
            homefImgf.setOnClickListener(this);
            homefImgs.setOnClickListener(this);
            homefImgt.setOnClickListener(this);
            homefImgo.setOnClickListener(this);
            //homeOFchange = (TextView) itemView.findViewById(R.id.homeOFchange);
            homeOFmore = (CardView) itemView.findViewById(R.id.homeOFmore);
            homeOFimage1 = (NetworkImageView) itemView.findViewById(R.id.homeOFimage1);
            homeOFimage2 = (NetworkImageView) itemView.findViewById(R.id.homeOFimage2);
            homeOFimage3 = (NetworkImageView) itemView.findViewById(R.id.homeOFimage3);
            homeOFimage4 = (NetworkImageView) itemView.findViewById(R.id.homeOFimage4);
            homeOFtitle1 = (TextView) itemView.findViewById(R.id.homeOFtitle1);
            homeOFtitle2 = (TextView) itemView.findViewById(R.id.homeOFtitle2);
            homeOFtitle3 = (TextView) itemView.findViewById(R.id.homeOFtitle3);
            homeOFtitle4 = (TextView) itemView.findViewById(R.id.homeOFtitle4);
            homeOFpl1 = (TextView) itemView.findViewById(R.id.homeOFpl1);
            homeOFpl2 = (TextView) itemView.findViewById(R.id.homeOFpl2);
            homeOFpl3 = (TextView) itemView.findViewById(R.id.homeOFpl3);
            homeOFpl4 = (TextView) itemView.findViewById(R.id.homeOFpl4);
            homeOFzan1 = (TextView) itemView.findViewById(R.id.homeOFzan1);
            homeOFzan2 = (TextView) itemView.findViewById(R.id.homeOFzan2);
            homeOFzan3 = (TextView) itemView.findViewById(R.id.homeOFzan3);
            homeOFzan4 = (TextView) itemView.findViewById(R.id.homeOFzan4);
            homeOFmore.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.homeOFmore:
                    if (entities != null) {
                        HomePageItem.ListsEntity b = entities.get(v0Position);
                        if (itemClickListener != null) {
                            itemClickListener.homePageItemClick(b);
                        }
                    }
                    break;
                case R.id.homefImgf:
                    if (entities.get(0).getTheme_list() != null) {
                        HomePageItem.ListsEntity.ThemeListEntity hlt = entities.get(0).getTheme_list().get(0);
                        if (itemClickListener != null) {
                            itemClickListener.homeItemClick(hlt);
                        }
                    }
                    break;
                case R.id.homefImgs:
                    if (entities.get(0).getTheme_list() != null) {
                        HomePageItem.ListsEntity.ThemeListEntity hlt = entities.get(0).getTheme_list().get(1);
                        if (itemClickListener != null) {
                            itemClickListener.homeItemClick(hlt);
                        }
                    }
                    break;
                case R.id.homefImgt:
                    if (entities.get(0).getTheme_list() != null) {
                        HomePageItem.ListsEntity.ThemeListEntity hlt = entities.get(0).getTheme_list().get(2);
                        if (itemClickListener != null) {
                            itemClickListener.homeItemClick(hlt);
                        }
                    }
                    break;
                case R.id.homefImgo:
                    if (entities.get(0).getTheme_list() != null) {
                        HomePageItem.ListsEntity.ThemeListEntity hlt = entities.get(0).getTheme_list().get(3);
                        if (itemClickListener != null) {
                            itemClickListener.homeItemClick(hlt);
                        }
                    }
                    break;

                    }

            }
        }

        public class ViewHolder01 extends RecyclerView.ViewHolder implements View.OnClickListener {
            private NetworkImageView homeTSimage1, homeTSimage2, homeTSimage3,
                    homeTSimage4, homeTSimage5, homeTSimage6;
            private TextView homeTStitle1, homeTStitle2, homeTStitle3,
                    homeTStitle4, homeTStitle5, homeTStitle6,
                    homeTscontent1, homeTscontent2, homeTscontent3,
                    homeTscontent4, homeTscontent5, homeTscontent6, homeOFMaintitle;
            private CardView homeTSmore;
            private int v1Position;

            public ViewHolder01(View itemView) {
                super(itemView);
                homeOFMaintitle = (TextView) itemView.findViewById(R.id.homeOFMaintitle);
                homeTSimage1 = (NetworkImageView) itemView.findViewById(R.id.homeTSimage1);
                homeTSimage2 = (NetworkImageView) itemView.findViewById(R.id.homeTSimage2);
                homeTSimage3 = (NetworkImageView) itemView.findViewById(R.id.homeTSimage3);
                homeTSimage4 = (NetworkImageView) itemView.findViewById(R.id.homeTSimage4);
                homeTSimage5 = (NetworkImageView) itemView.findViewById(R.id.homeTSimage5);
                homeTSimage6 = (NetworkImageView) itemView.findViewById(R.id.homeTSimage6);
                homeTStitle1 = (TextView) itemView.findViewById(R.id.homeTStitle1);
                homeTStitle2 = (TextView) itemView.findViewById(R.id.homeTStitle2);
                homeTStitle3 = (TextView) itemView.findViewById(R.id.homeTStitle3);
                homeTStitle4 = (TextView) itemView.findViewById(R.id.homeTStitle4);
                homeTStitle5 = (TextView) itemView.findViewById(R.id.homeTStitle5);
                homeTStitle6 = (TextView) itemView.findViewById(R.id.homeTStitle6);
                homeTscontent1 = (TextView) itemView.findViewById(R.id.homeTscontent1);
                homeTscontent2 = (TextView) itemView.findViewById(R.id.homeTscontent2);
                homeTscontent3 = (TextView) itemView.findViewById(R.id.homeTscontent3);
                homeTscontent4 = (TextView) itemView.findViewById(R.id.homeTscontent4);
                homeTscontent5 = (TextView) itemView.findViewById(R.id.homeTscontent5);
                homeTscontent6 = (TextView) itemView.findViewById(R.id.homeTscontent6);
                homeTSmore = (CardView) itemView.findViewById(R.id.homeTSmore);
                homeTSmore.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                if (entities != null) {
                    HomePageItem.ListsEntity b = entities.get(v1Position);
                    if (itemClickListener != null) {
                        itemClickListener.homePageItemClick(b);
                    }
                }
            }
        }

        public class ViewHolder02 extends RecyclerView.ViewHolder implements View.OnClickListener {
            private TextView homeTTcontent1, homeTTcontent2, homeTTtitle;
            private NetworkImageView homeTTimage1, homeTTimage2;
            private CardView homeTTmore;
            private int v2Position;

            public ViewHolder02(View itemView) {
                super(itemView);
                homeTTcontent1 = (TextView) itemView.findViewById(R.id.homeTTcontent1);
                homeTTcontent2 = (TextView) itemView.findViewById(R.id.homeTTcontent2);
                homeTTimage1 = (NetworkImageView) itemView.findViewById(R.id.homeTTimage1);
                homeTTimage2 = (NetworkImageView) itemView.findViewById(R.id.homeTTimage2);
                homeTTtitle = (TextView) itemView.findViewById(R.id.homeTTtitle);
                homeTTmore = (CardView) itemView.findViewById(R.id.homeTTmore);
                homeTTmore.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                if (entities != null) {
                    HomePageItem.ListsEntity b = entities.get(v2Position);
                    if (itemClickListener != null) {
                        itemClickListener.homePageItemClick(b);
                    }
                }
            }
        }

        public class ViewHolder03 extends RecyclerView.ViewHolder implements View.OnClickListener {
            private NetworkImageView homeOFimage1, homeOFimage2, homeOFimage3, homeOFimage4;
            private TextView homeOFtitle1, homeOFtitle2, homeOFtitle3, homeOFtitle4, homeOFpl1, homeOFchange,
                    homeOFpl2, homeOFpl3, homeOFpl4, homeOFzan1, homeOFzan2, homeOFzan3, homeOFzan4,
                    homeOFMaintitle;
            private int v3Position;
            private CardView homeOFmore;

            public ViewHolder03(View itemView) {
                super(itemView);
                homeOFMaintitle = (TextView) itemView.findViewById(R.id.homeOFMaintitle);
                //homeOFchange = (TextView) itemView.findViewById(R.id.homeOFchange);
                homeOFmore = (CardView) itemView.findViewById(R.id.homeOFmore);
                homeOFimage1 = (NetworkImageView) itemView.findViewById(R.id.homeOFimage1);
                homeOFimage2 = (NetworkImageView) itemView.findViewById(R.id.homeOFimage2);
                homeOFimage3 = (NetworkImageView) itemView.findViewById(R.id.homeOFimage3);
                homeOFimage4 = (NetworkImageView) itemView.findViewById(R.id.homeOFimage4);
                homeOFtitle1 = (TextView) itemView.findViewById(R.id.homeOFtitle1);
                homeOFtitle2 = (TextView) itemView.findViewById(R.id.homeOFtitle2);
                homeOFtitle3 = (TextView) itemView.findViewById(R.id.homeOFtitle3);
                homeOFtitle4 = (TextView) itemView.findViewById(R.id.homeOFtitle4);
                homeOFpl1 = (TextView) itemView.findViewById(R.id.homeOFpl1);
                homeOFpl2 = (TextView) itemView.findViewById(R.id.homeOFpl2);
                homeOFpl3 = (TextView) itemView.findViewById(R.id.homeOFpl3);
                homeOFpl4 = (TextView) itemView.findViewById(R.id.homeOFpl4);
                homeOFzan1 = (TextView) itemView.findViewById(R.id.homeOFzan1);
                homeOFzan2 = (TextView) itemView.findViewById(R.id.homeOFzan2);
                homeOFzan3 = (TextView) itemView.findViewById(R.id.homeOFzan3);
                homeOFzan4 = (TextView) itemView.findViewById(R.id.homeOFzan4);
                homeOFmore.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                if (entities != null) {
                    HomePageItem.ListsEntity b = entities.get(v3Position);
                    if (itemClickListener != null) {
                        itemClickListener.homePageItemClick(b);
                    }
                }
            }
        }
    }
